/**
 * 
 */
package br.com.plurianual.model.dao.impl;

import javax.persistence.Query;

import br.com.plurianual.exception.DataAccessLayerException;
import br.com.plurianual.model.dao.OrgaoDAO;
import br.com.plurianual.model.entity.Orgao;

/**
 * @author FRAGA
 *
 */
public class OrgaoDAOImpl extends DAOImpl<Orgao, Integer> implements
		OrgaoDAO {

	/*
	 * (non-Javadoc)
	 * 
	 * @see br.com.fraga.model.dao.impl.DAO#delete(T)
	 */
	@Override
	public final void deleteAndClearOrphans(Orgao orgao) throws DataAccessLayerException {
		try {
			log.debug("Delete object: " + getPersistentClass().getName());
			
			Query clearUnidadeOrcamentariaQuery = this.getEntityManager().createNamedQuery("deleteOrgao.clearUnidadeOrcamentaria");
			Query clearProgramaQuery = this.getEntityManager().createNamedQuery("deleteOrgao.clearPrograma");
			Query clearAcaoQuery = this.getEntityManager().createNamedQuery("deleteOrgao.clearAcao");

			clearUnidadeOrcamentariaQuery.setParameter("orgao", orgao);
			clearProgramaQuery.setParameter("orgao", orgao);
			clearAcaoQuery.setParameter("orgao", orgao);
			
			clearUnidadeOrcamentariaQuery.executeUpdate();
			clearProgramaQuery.executeUpdate();
			clearAcaoQuery.executeUpdate();
			
			this.delete(orgao);
		} catch (Exception e) {
			handleException(e);
		}
	}
}
